package com.tiancai.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiancai.context.BaseContext;
import com.tiancai.mapper.UserMapper;
import com.tiancai.pojo.po.Comment;
import com.tiancai.mapper.CommentMapper;
import com.tiancai.pojo.po.User;
import com.tiancai.pojo.po.UserCommentLike;
import com.tiancai.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiancai.service.IUserCommentLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tiancai
 * @since 2025-03-11
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IUserCommentLikeService userCommentLikeService;
    @Override

    public Comment addNewComment(Long workId, String content, Long commentId) {
        User user = userMapper.selectById(BaseContext.getCurrentId());
        Comment comment = Comment.builder()
                .userId(BaseContext.getCurrentId())
                .username(user.getUsername())
                .userAvatar(user.getAvatar())
                .workId(workId)
                .content(content)
                .commentId(commentId)
                .build();
        commentMapper.insert(comment);
            return comment;
    }

    @Override
    public void like(Long commentId) {
        Comment comment = commentMapper.selectById(commentId);
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("id", comment.getUserId()));
        //查找用户评论点赞表,如果存在则更新点赞数，不存在则插入一条数据
        QueryWrapper<UserCommentLike> wrapper = new QueryWrapper<UserCommentLike>()
                .eq("user_id", BaseContext.getCurrentId())
                .eq("comment_id", commentId);
        if (userCommentLikeService.getOne(wrapper) != null) {
            //如果存在则删除点赞数
            comment.setLikes(comment.getLikes()-1);
            commentMapper.updateById(comment);
            user.setOwnLikeCount(user.getOwnLikeCount()-1);
            userMapper.updateById(user);
            userCommentLikeService.remove(wrapper);
        } else {
            //如果不存在则插入一条数据
            comment.setLikes(comment.getLikes()+1);
            commentMapper.updateById(comment);
            user.setOwnLikeCount(user.getOwnLikeCount()+1);
            userMapper.updateById(user);
            UserCommentLike userCommentLike = UserCommentLike.builder()
                    .userId(BaseContext.getCurrentId())
                    .commentId(commentId)
                    .build();
            userCommentLikeService.save(userCommentLike);
        }
    }

    @Override
    public List<Comment> getCommentByCommentId(Long commentId) {
        List<Comment> commentList = commentMapper.selectList(new QueryWrapper<Comment>().eq("comment_id", commentId));
        return commentList;
    }

    @Override
    public Integer removeAll(Long commentId) {
        //删除评论，如果评论下有子评论，则一并删除
        //查看当前用户是否是评论的用户
        Comment comment = commentMapper.selectById(commentId);
        if (comment.getUserId().equals(BaseContext.getCurrentId())){
            //是评论的用户，删除评论
            commentMapper.delete(new QueryWrapper<Comment>().eq("comment_id", commentId));
            commentMapper.delete(new QueryWrapper<Comment>().eq("id", commentId));
            return 1;
        }
        //如果不等则提示没有权限
        else{
            return 0;
        }
    }
}
